﻿using Customer.API.Applcation.Command.ProcurementCommand;
using Customer.API.Applcation.Command.RefundCommand;
using Customer.API.Applcation.Command.SalesCommand;
using Customer.API.Dtos;
using Customer.Domain.Dto;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace Customer.API.Controllers
{

    /// <summary>
    /// 销售管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    
    public class SalesController : ControllerBase
    {

        IMediator mediator;

        public SalesController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加销售
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddSales(SalesAddDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.User_Id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                command.addSales.FirstOrDefault().Office_Id = sid;
                command.addSales.FirstOrDefault().User_Id = Convert.ToInt32(uid);
                result.Data = await mediator.Send(new SalesAddCommand { AddDto= command } );
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除采购单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> DelSalse(SalesDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.User_Id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                result.Data = await mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改销售
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdSales(SalesAddDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.User_Id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                command.addSales.FirstOrDefault().Office_Id = sid;
                command.addSales.FirstOrDefault().User_Id = Convert.ToInt32(uid);
                result.Data = await mediator.Send(new  SalesUpdCommand{AddDto= command });
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }





    }
}
